package com.rockxin.cat;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.orhanobut.logger.Logger;
import com.rockxin.cat.adapter.PagerAdapter;
import com.rockxin.cat.base.BaseActivity;
import com.rockxin.cat.customview.NoScrollViewPager;
import com.rockxin.cat.event.JumpIndexEvent;
import com.rockxin.cat.fragment.FindFragment;
import com.rockxin.cat.fragment.HomeFragment;
import com.rockxin.cat.fragment.MeFragment;
import com.rockxin.cat.fragment.MoneyFragment;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabReselectListener;
import com.roughike.bottombar.OnTabSelectListener;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class MainActivity extends BaseActivity implements OnTabSelectListener, OnTabReselectListener {

    @BindView(R.id.mBottomBar)
    BottomBar mBottomBar;
    @BindView(R.id.viewPager)
    NoScrollViewPager viewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initData() {
        EventBus.getDefault().register(this);
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(HomeFragment.newInstance());
        fragmentList.add(MoneyFragment.newInstance());
        fragmentList.add(FindFragment.newInstance());
        fragmentList.add(MeFragment.newInstance());
        PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), fragmentList);

        viewPager.setAdapter(adapter);
        viewPager.setNoScroll(true);
        viewPager.setOffscreenPageLimit(fragmentList.size());
        viewPager.setCurrentItem(3);
        mBottomBar.setOnTabSelectListener(this);
        mBottomBar.setOnTabReselectListener(this);


    }


    @Override
    public void onTabSelected(int tabId) {
        switch (tabId) {
            case R.id.home:
                viewPager.setCurrentItem(0, false);
                break;
            case R.id.money:
                viewPager.setCurrentItem(1, false);
                break;
            case R.id.find:
                viewPager.setCurrentItem(2, false);
                break;
            case R.id.me:
                viewPager.setCurrentItem(3, false);
                break;
        }
    }

    @Override
    public void onTabReSelected(int tabId) {
        switch (tabId) {
            case R.id.home:
                viewPager.setCurrentItem(0, false);
                break;
            case R.id.money:
                viewPager.setCurrentItem(1, false);
                break;
            case R.id.find:
                viewPager.setCurrentItem(2, false);
                break;
            case R.id.me:
                viewPager.setCurrentItem(3, false);
                break;
        }
    }

    @org.simple.eventbus.Subscriber(tag = "home_money")
    public void getPageEvent(String str) {
        mBottomBar.selectTabAtPosition(1);
    }

}
